package com.yndsoft.jc.supplier.order.entry;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.model.SupplierResult;
import com.yndsoft.jc.model.SupplierResultImg;
import com.yndsoft.jc.supplier.adapter.ImageAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.modelput.Entry;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.order.OnDeleteListener;
import com.yndsoft.jc.supplier.utils.Base64UploadImg;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.imageselector.ImgSelActivity;

import java.util.ArrayList;
import java.util.List;

public class OrderEntryActivity extends SupplierBaseActivity implements View.OnClickListener {

    private EditText result;
    private RecyclerView images;
    private ImageAdapter imageAdapter;
    private String id;
    private String odid;
    private List<Entry.Imgs> imgsTemp = new ArrayList<>();;

    @Override
    public int getContentResId() {
        return R.layout.activity_order_entry;
    }

    public static void start(Context context, String id, String odetid) {
        Intent intent = new Intent(context, OrderEntryActivity.class);
        intent.putExtra(Constants.ID, id);
        intent.putExtra(Constants.ODID, odetid);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        Button barSave = findViewById(R.id.bar_save);
        barSave.setVisibility(View.VISIBLE);
        result = findViewById(R.id.result);
        images = findViewById(R.id.images);
        barTitle.setText("录入结果");
        barBack.setOnClickListener(this);
        barSave.setOnClickListener(this);
    }

    private void setTempImg(String path) {
        Entry.Imgs imgs = new Entry.Imgs();
        imgs.setImg(path);
        imgsTemp.add(imgs);
    }

    @Override
    public void initData() {
        id = getIntent().getStringExtra(Constants.ID);
        odid = getIntent().getStringExtra(Constants.ODID);
        mHttpUtil.entryDetail(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                if (1 == returnMsg.getCode()) {
                    String info = returnMsg.getInfo();
                    if (null != info && !"".equals(info)) {
                        SupplierResult supplierResult = new Gson().fromJson(info, SupplierResult.class);
                        if (supplierResult != null) {
                            result.setText(supplierResult.getNote() != null ? supplierResult.getNote() : "");
                            List<SupplierResultImg> dets = supplierResult.getDets();
                            if (dets != null && dets.size() > 0) {
                                List<String> imgList = new ArrayList<>();
                                for (int i = 0; i < dets.size(); i++) {
                                    SupplierResultImg supplierResultImg = dets.get(i);
                                    if (supplierResultImg != null) {
                                        String img = supplierResultImg.getImg();
                                        if (img != null) {
                                            imgList.add(img);
                                            setTempImg(img);
                                        }
                                    }
                                }
                                loadAdapter(imgList);
                            }
                        }
                    } else {
                        loadAdapter(new ArrayList<String>());
                    }
                } else {
                    loadAdapter(new ArrayList<String>());
                }
            }
        }, "entry_sign", id, odid, mSession.getSid());

        loadAdapter(new ArrayList<String>());
    }

    private void loadAdapter(List<String> imgList) {
        imgList.add(Constants.PLACE_HOLDER);
        images.setLayoutManager(new GridLayoutManager(mContext, 5));
        imageAdapter = new ImageAdapter(mContext, OrderEntryActivity.this, imgList);
        images.setAdapter(imageAdapter);
        imageAdapter.setOnDeleteListener(new OnDeleteListener() {
            @Override
            public void onDelete(int position) {
                if (imgsTemp != null && imgsTemp.size() > 0 && position < imgsTemp.size()) {
                    imgsTemp.remove(position);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.bar_save:
                String resultValue = result.getText().toString().trim();
                if (TextUtils.isEmpty(resultValue)) {
                    showHint("请输入检测结果！");
                } else {
                    if (imgsTemp != null && imgsTemp.size() > 0) {
                        Entry entry = new Entry();
                        entry.setOid(id);
                        entry.setOdetid(odid);
                        entry.setSid(mSession.getSid());
                        entry.setCreateadmin(mSession.getId());
                        entry.setNote(resultValue);
                        entry.setDets(imgsTemp);
                        String json = new Gson().toJson(entry);
                        mHttpUtil.entrySignOrder(new ProgressSubscriber(mContext) {
                            @Override
                            public void next(ReturnMsg returnMsg) {
                                if (1 == returnMsg.getCode()) {
                                    String sum = mCache.getAsString(Constants.SUM);
                                    StringBuilder sb = new StringBuilder();
                                    if (sum != null) {
                                        if (sum.contains(odid)) {
                                            sb.append(sum);
                                        } else {
                                            sb.append(sum).append(odid).append(",");
                                        }
                                    } else {
                                        sb.append(odid).append(",");
                                    }
                                    mCache.put(Constants.SUM, sb.toString());
                                    ToastUtil.show(mContext, "操作成功！");
                                    finish();
                                } else {
                                    ToastUtil.show(mContext, "操作失败！");
                                }
                            }
                        }, "entry", id, mSession.getSid(), json);
                    } else {
                        showHint("请上传详情图片！");
                    }
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case Constants.REQUEST_ENTRY_CODE:
                    List<String> pathList = data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
                    if (pathList != null && pathList.size() > 0) {
                        String path = pathList.get(0);
                        if (path != null) {
                            Base64UploadImg base64UploadImg = new Base64UploadImg();
                            base64UploadImg.uploadImg(mContext, path);
                            base64UploadImg.setOnUpImgListener(new Base64UploadImg.OnUpImgListener() {
                                @Override
                                public void onUpImg(String imgBase64) {
                                    List<String> imgList = new ArrayList<>();
                                    imgList.addAll(imageAdapter.getImgList());
                                    imgList.add(imgBase64);
                                    setTempImg(imgBase64);
                                    loadAdapter(imgList);
                                }
                            });
                        }
                    }
                    break;
            }
        }
    }
}
